package org.linlinjava.litemall.wx.web;

import static org.linlinjava.litemall.wx.util.WxResponseCode.COMMON_DETAIL_IS_NULL;

import java.time.LocalDateTime;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallFormSupply;
import org.linlinjava.litemall.db.service.SupplyService;
import org.linlinjava.litemall.db.util.SequenceUtils;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxQualificationsService;
import org.linlinjava.litemall.wx.service.WxSupplyService;
import org.linlinjava.litemall.wx.vo.QualificationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 药材供应
 */
@RestController
@RequestMapping("/wx/supply")
@Validated
public class WxSupplyController {
	private final Log logger = LogFactory.getLog(WxSupplyController.class);

	@Autowired
	private SupplyService supplyService;
	@Autowired
	private WxSupplyService wxSupplyService;
	@Autowired
	private WxQualificationsService wxQualificationsService;

	@GetMapping("list")
	public Object list(@LoginUser Integer userId, @NotNull Byte type, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order,
			@RequestParam(defaultValue = "") String searchValue) {

		return wxSupplyService.list(userId, type, page, limit, sort, order, searchValue);
	}

	@GetMapping("myList")
	public Object myList(@LoginUser Integer userId, @NotNull Byte type, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order,
			@RequestParam(defaultValue = "") String searchValue) {

		return wxSupplyService.myList(userId, type, page, limit, sort, order,searchValue);
	}

	@GetMapping("detail")
	public Object detail(@LoginUser Integer userId, @NotNull Integer id) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		return wxSupplyService.detail(userId, id);
	}

	private Object validate(LitemallFormSupply formSupply) {
		return null;

	}

	@PostMapping("save")
	public Object save(@LoginUser Integer userId, @RequestBody LitemallFormSupply formSupply) {
		System.out.println(formSupply.toString());
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		Object error = validate(formSupply);
		if (error != null) {
			return error;
		}

		QualificationsVo qualificationsVo = wxQualificationsService.getQualificationsVo(userId);
		if (qualificationsVo == null) {
			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "获取用户资质信息异常，请刷新再试");
		}

//		if (StringUtils.isBlank(formSupply.getPrice())) {
//			formSupply.setPrice("面议");
//		}
//		if (StringUtils.isBlank(formSupply.getBillDemand())) {
//			formSupply.setBillDemand("无");
//		}
//		if (StringUtils.isBlank(formSupply.getQualificationRequirements())) {
//			formSupply.setQualificationRequirements("无");
//		}
//		if (StringUtils.isBlank(formSupply.getPackingMethod())) {
//			formSupply.setPackingMethod("可商议");
//		}
//		if (StringUtils.isBlank(formSupply.getPayMethod())) {
//			formSupply.setPayMethod("可商议");
//		}
//		if (StringUtils.isBlank(formSupply.getPayMethod())) {
//			formSupply.setPayMethod("可商议");
//		}

		formSupply.setCode(SequenceUtils.getSupplySequenceCode());
		formSupply.setShopId(qualificationsVo.getId());
		formSupply.setShopName(qualificationsVo.getCompanyName());
		formSupply.setPublishDt(LocalDateTime.now());
		formSupply.setStatus((byte) 0);

		formSupply.setId(null);
		formSupply.setUserId(userId);
		supplyService.add(formSupply);

		return ResponseUtil.ok();
	}

	@GetMapping("delete")
	public Object delete(@NotNull Integer id,@LoginUser Integer userId) {
		return supplyService.delete(userId,id);
	}
}